import JSEncrypt from 'jsencrypt';

// 密钥对
const publicKey : string = 'MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAKoR8mX0rGKLqzcWmOzbfj64K8ZIgOdH\n' +
	'nzkXSOVOZbFu/TJhZ7rFAN+eaGkl3C4buccQd/EjEsj9ir7ijT7h96MCAwEAAQ==';

const privateKey : string = 'MIIBVAIBADANBgkqhkiG9w0BAQEFAASCAT4wggE6AgEAAkEAqhHyZfSsYourNxaY\n' +
	'7Nt+PrgrxkiA50efORdI5U5lsW79MmFnusUA355oaSXcLhu5xxB38SMSyP2KvuKN\n' +
	'PuH3owIDAQABAkAfoiLyL+Z4lf4Myxk6xUDgLaWGximj20CUf+5BKKnlrK+Ed8gA\n' +
	'kM0HqoTt2UZwA5E2MzS4EI2gjfQhz5X28uqxAiEA3wNFxfrCZlSZHb0gn2zDpWow\n' +
	'cSxQAgiCstxGUoOqlW8CIQDDOerGKH5OmCJ4Z21v+F25WaHYPxCFMvwxpcw99Ecv\n' +
	'DQIgIdhDTIqD2jfYjPTY8Jj3EDGPbH2HHuffvflECt3Ek60CIQCFRlCkHpi7hthh\n' +
	'YhovyloRYsM+IS9h/0BzlEAuO0ktMQIgSPT3aFAgJYwKpqRYKlLDVcflZFCKY7u3\n' +
	'UP8iWi1Qw0Y=';

// 加密函数，支持 string 和 number 类型的输入
export function encrypt(txt : string | number | undefined) : string | false {
	if (txt === undefined) {
		throw new Error("不能加密 undefined 数据");
	}

	const stringData = String(txt);  // 将输入的数据转换为字符串

	const encryptor = new JSEncrypt();
	encryptor.setPublicKey(publicKey); // 设置公钥
	return encryptor.encrypt(stringData) || false; // 对数据进行加密，返回加密后的字符串或 false
}

// 解密函数，不需要修改
export function decrypt(txt : string) : string | false {
	const encryptor = new JSEncrypt();
	encryptor.setPrivateKey(privateKey); // 设置私钥
	return encryptor.decrypt(txt) || false; // 对数据进行解密，返回解密后的字符串或 false
}